package wireless.frequent.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import wireless.frequent.pojo.Frequent;
import wireless.util.GetAndSave;

public class Load {

	private String filePath;

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	
	public String execute() throws Exception{
		List<Frequent> list = findAll(filePath);
//		for(Iterator sit=list.iterator();sit.hasNext();)//迭代取值
//	     {
//			 Frequent frequent=(Frequent)sit.next();
//			 GetFrequentList dao = new GetFrequentList();
		GetAndSave<Frequent> dao =new GetAndSave("Frequent");
		dao.saveEntityList(list);
//		dao.saveFrequentList(list);
//	     }
		
		return "success";
	}
	
	private List<Frequent> findAll(String filePath){
		 	File file = new File(filePath);
	        List<Frequent> list = new ArrayList<Frequent>();
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 0;
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
	                System.out.println("line " + line + ": " + tempString);
	                Frequent frequent = new Frequent();
	                frequent.setFid(Integer.parseInt(tempString.split(" ")[0]));
	                frequent.setValue(tempString.split(" ")[1]);
	                frequent.setCount(Integer.parseInt(tempString.split(" ")[2]));
	                frequent.setLength(Integer.parseInt(tempString.split(" ")[3]));
	                
	                list.add(frequent);
	                line++;
	            }
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
	        return list;
	}
}
